package com.bdqn.oa.dao.impl;

import com.bdqn.oa.dao.ReimbursementFileDao;
import com.bdqn.oa.entity.ReimbursementFile;
import com.bdqn.oa.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

/**
 * @author Chenrz
 * @date 2022/7/20 20:11
 */
public class ReimbursementFileDaoImpl implements ReimbursementFileDao {
    QueryRunner queryRunner = new QueryRunner();
    /**
     * 新增附件
     *
     * @param reiFile
     * @return
     * @throws SQLException
     */
    @Override
    public int saveReiFile(ReimbursementFile reiFile) throws SQLException {
        String sql = "insert into `reimbursement_file`(reimbursement_id,url) " +
                " values (?,?);";
        Object[] params = {
                reiFile.getReimbursementId(),reiFile.getUrl()
        };
        return queryRunner.update(DruidUtils.getConnection(),sql,params);
    }

    /**
     * 删除附件
     *
     * @param reimbursementId
     * @return
     * @throws SQLException
     */
    @Override
    public int deleteReiFile(Integer reimbursementId) throws SQLException {
        String sql = "delete from `reimbursement_file` where reimbursement_id = ?;";
        return queryRunner.update(DruidUtils.getConnection(),sql,reimbursementId);
    }

    /**
     * 修改附件
     *
     * @param reiFile
     * @return
     * @throws SQLException
     */
    @Override
    public int updateReiFile(ReimbursementFile reiFile) throws SQLException {
        String sql = "update `reimbursement_file` set `url`=? where reimbursement_id = ?;";
        Object[] params = {
                reiFile.getReimbursementId(),reiFile.getUrl()
        };
        return queryRunner.update(DruidUtils.getConnection(),sql,params);
    }

    /**
     * 查询附件
     *
     * @param reimbursementId
     * @return
     * @throws SQLException
     */
    @Override
    public ReimbursementFile getReiFile(Integer reimbursementId) throws SQLException {
        ReimbursementFile reimbursementFile = new ReimbursementFile();
        String sql = "select `id`,`reimbursement_id`,`url` from `reimbursement_file` where reimbursement_id = ?;";
        reimbursementFile = queryRunner.query(DruidUtils.getConnection(),sql,new BeanHandler<ReimbursementFile>(ReimbursementFile.class),reimbursementId);
        return reimbursementFile;
    }
}
